﻿using System;
using System.Reflection;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CodeEndeavors.Templates.Wizard
{
    public partial class UserInputForm : Form
    {
        public UserInputForm()
        {
            InitializeComponent();
            folderBrowserDialog1.SelectedPath = Properties.Settings.Default.DotNetNukeDir;
            txtCompanyNamespace.Text = Properties.Settings.Default.CompanyNamespace;
            txtDotNetNukeDir.Text = Properties.Settings.Default.DotNetNukeDir;
            txtOrganization.Text = Properties.Settings.Default.Organization;
            txtOwner.Text = Properties.Settings.Default.OwnerName;
            txtSupportUrl.Text = Properties.Settings.Default.SupportUrl;
            txtEmail.Text = Properties.Settings.Default.SupportEmail;
        }

        private bool validForm()
        {
            string err = "";
            string dnnDir = txtDotNetNukeDir.Text;
            if (txtCompanyNamespace.Text.IndexOf(" ") > -1)    //todo:  use regex
                err = "Invalid Namespace";
            
            if (Directory.Exists(dnnDir))
            {
                err = fileExists(dnnDir, "bin\\dotnetnuke.dll");
                if (string.IsNullOrEmpty(err))
                    err = fileExists(dnnDir, "bin\\DotNetNuke.WebUtility.dll");
                if (string.IsNullOrEmpty(err))
                    err = fileExists(dnnDir, "bin\\Microsoft.ApplicationBlocks.Data.dll");
                if (string.IsNullOrEmpty(err))
                    err = fileVersion(dnnDir, "bin\\DotNetNuke.WebUtility.dll", new Version(4, 0));

            }
            else
                err = string.Format("DotNetNuke Directory {0} does not exist.  Please select the location of your dotnetnuke installation", dnnDir);

            if (!string.IsNullOrEmpty(err))
            {
                MessageBox.Show(err);
                return false;
            }
            else
            {
                err = fileExists(dnnDir, "bin\\System.Web.Extensions.dll");
                return MessageBox.Show(err + "\r\nIf you have .NET 3.5 SP1 or Microsoft AJAX 1.0 installed this dll is not needed in the DotNetNuke directory.  Do you have either one of these packages installed?", "Warning", MessageBoxButtons.YesNo) == DialogResult.Yes; 
            }

        }

        private string fileExists(string dir, string fileName)
        {
            if (!File.Exists(Path.Combine(dir, fileName)))
                return string.Format("File does not exist ({0}).  DotNetNuke directory invalid.", fileName);
            return "";
        }

        private string fileVersion(string dir, string fileName, Version version)
        {
            Assembly a = Assembly.LoadFile(Path.Combine(dir, fileName));
            if (a.GetName().Version < version)
                return string.Format("Version of file {0} needs to be version {1} or later.  Version found {2}.", fileName, version, a.GetName().Version);
            return "";
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;   
            this.Dispose();
        }

        private void picLogo_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.codeendeavors.com");
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (validForm())
            {
                Properties.Settings.Default.DotNetNukeDir = txtDotNetNukeDir.Text;
                Properties.Settings.Default.CompanyNamespace = txtCompanyNamespace.Text;

                Properties.Settings.Default.Organization = txtOrganization.Text;
                Properties.Settings.Default.OwnerName = txtOwner.Text;
                Properties.Settings.Default.SupportUrl = txtSupportUrl.Text;
                Properties.Settings.Default.SupportEmail = txtEmail.Text;
                Properties.Settings.Default.Save();
                this.DialogResult = DialogResult.OK;
                this.Dispose();
            }
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                txtDotNetNukeDir.Text = folderBrowserDialog1.SelectedPath;
            }

        }

    }
}
